package com.example.demo.controller;

import com.example.demo.config.JwtUtil;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.util.ReturnEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.tomcat.jni.Local;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.TemporalField;
import java.util.Date;

@Api(value = "首页接口", tags = "Shiro", produces = "application/json")
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "请求正常完成"),
        @ApiResponse(code = 400, message = "请求中有语法问题，或不能满足请求"),
        @ApiResponse(code = 500, message = "服务器出现异常")})
@RestController
@RequestMapping("/ga")
public class IndexController {
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
    @Resource
    UserService userService;

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息", produces = "application/json")
    @PostMapping("/userselfinfo")
    public ReturnEntity userselfinfo(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (StringUtils.isBlank(token)) {
            return null;
        }
        User user = JwtUtil.verifyToken(token);
        User user1 = userService.getUser(user.getUsername());
        ReturnEntity returnEntity = new ReturnEntity("200", "成功", user1, new Timestamp(new Date().getTime()).toString());
        logger.info(returnEntity.toString());
        return returnEntity;

    }
//
//    @PostMapping("/login")
//    public String login(Map<String, String> map) {
//        map.put("msg", "请登录");q
//        return "login";
//
//    }
//
//    @GetMapping("/loginout")
//    public String loginout(Map<String, String> map) {
//        Subject subject = SecurityUtils.getSubject();
//        subject.logout();
//        map.put("msg", "退出成功请重新登录");
//        return "login";
//
//    }

    @ApiOperation(value = "注册", notes = "用户注册", produces = "application/json")
    @PostMapping("/register")
    public User register(@RequestBody User user) {
        userService.register(user);
        return user;

    }

    @ApiOperation(value = "登录", notes = "用户登录", produces = "application/json")
    @PostMapping(value = "/login1")
    public String login1(@RequestBody User user) {
        Subject subject = SecurityUtils.getSubject();
        //基础规则校验用户名 长度之类
        //如果库中密码是加密的,在此处加密之后再入下面方法
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(user.getUsername(), user.getPassword());
//            usernamePasswordToken
        try {
            subject.login(usernamePasswordToken);
            String token = JwtUtil.createToken(user);
            return token;
        } catch (Exception e) {
            return "用户名或密码错误";
        }
    }
}
